package utfpr.gang5.hermes.controledepresenca.cliente.swt;

import java.rmi.RemoteException;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import utfpr.gang5.hermes.controledepresenca.cliente.RmiServiceFactory;
import org.eclipse.swt.widgets.List;

public class CursoDialog extends TitleAreaDialog {
	private Text txNome;
	private Text txNVagas;
	
	
	private boolean logou;
	private Object usuario;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public CursoDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		setTitle("Login");
		setMessage("Informe seu nome de usuário e senha");
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayout(new GridLayout(2, false));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		Label lblNomeDeUsurio = new Label(container, SWT.NONE);
		lblNomeDeUsurio.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNomeDeUsurio.setText("Nome do curso:");
		
		txNome = new Text(container, SWT.BORDER);
		txNome.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblSenha = new Label(container, SWT.NONE);
		lblSenha.setText("Nº vagas:");
		
		txNVagas = new Text(container, SWT.BORDER | SWT.PASSWORD);
		txNVagas.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
		
		Label lblDisciplinas = new Label(container, SWT.NONE);
		lblDisciplinas.setText("Disciplinas:");
		
		Composite composite = new Composite(container, SWT.NONE);
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_composite.widthHint = 274;
		composite.setLayoutData(gd_composite);

		return area;
	}
	
	@Override
	protected void okPressed() {
		String username = txNome.getText().trim();
		String senha = txNVagas.getText().trim();
		
		if (username.equals("") || senha.equals("")) {
			setErrorMessage("Nome de usuário ou senha incorretos");
			return;
		}
		
		
		try {
			usuario = RmiServiceFactory.getService().login(username, senha);
			if (usuario == null) {
				setErrorMessage("Nome de usuário ou senha incorretos");
				return;
			}
			
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		setLogou(true);
		super.okPressed();
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Entrar",
				true);
	
		createButton(parent, IDialogConstants.CANCEL_ID,
				"Sair", false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(414, 329);
	}

	public boolean isLogou() {
		return logou;
	}

	public void setLogou(boolean logou) {
		this.logou = logou;
	}
}
